package com.campusbook.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 自定义BeanPostProcessor，演示Spring容器扩展点
 * 用于记录所有Bean的初始化过程
 */
@Component
public class LoggingBeanPostProcessor implements BeanPostProcessor, PriorityOrdered {

    private static final Logger logger = LoggerFactory.getLogger(LoggingBeanPostProcessor.class);

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        logger.debug("Bean初始化前: {} -> {}", beanName, bean.getClass().getSimpleName());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        logger.info("Bean初始化完成: {} -> {}", beanName, bean.getClass().getSimpleName());
        
        // 演示对特定Bean的特殊处理
        if (beanName.contains("Service")) {
            logger.info("发现服务层Bean: {}", beanName);
        }
        
        return bean;
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
